#ifndef _RTAI_SHMEM_H_
#define _RTAI_SHMEM_H_

#define SHM_KEY 0x1234

#define MAX_VAR_NAME_LEN 32
#define HASH_TABLE_SIZE 256
#define MAX_VAR_NUM 128
#define SHMEM_SIZE (sizeof(SharedMemory))  // 共享内存大小

typedef enum {
    TYPE_INT,
    TYPE_DINT,
    TYPE_FLOAT,
    TYPE_DOUBLE,
    TYPE_STRING,
    TYPE_UINT,
    TYPE_UDINT
} DataType;


typedef union {
    int int32;
    long int64;
    float float32;
    double double64;
    char str[32];
    unsigned int uint32;
    unsigned long uint64;
} Value;

typedef struct {
    char name[MAX_VAR_NAME_LEN];
    Value value;
    DataType type;
} Data;

typedef struct {
    int index;
    char name[MAX_VAR_NAME_LEN];
} HashEntry;

typedef struct {
    HashEntry entries[HASH_TABLE_SIZE];
} HashTable;

typedef struct {
    unsigned int alias;
    unsigned int position;
    unsigned int vender_id;
    unsigned int product_code;
    unsigned int index;
    unsigned int subindex;
    unsigned int *offset;
    unsigned int *bit_position;
    unsigned int bitlen;
    char name[MAX_VAR_NAME_LEN-4];
} PDOConfig;

typedef struct {
    Data data[MAX_VAR_NUM];
    int index;
    HashTable hash_table;
    // PDOConfig pdos[MAX_VAR_NUM];
    PDOConfig rxpdo[MAX_VAR_NUM];
    PDOConfig txpdo[MAX_VAR_NUM];
    int rxpdo_count; // 实际 rxpdo 条目数
    int txpdo_count; // 实际 rxpdo 条目数
} SharedMemory;


// 共享内存指针
extern SharedMemory *shm_ptr;

#ifdef __IN_RTAI__
void rtai_shmem_init(void);
#else
void lxrt_shmem_init(void);
#endif

int shmem_register(char *name, DataType type, void *value);
int shmem_unregister(char *name);
int shmem_write(char *name, void *value);
int shmem_read(char *name, void *value);

#endif
